

//-----------------------显示列表-------------------------------------
function GetCategory() {
    $.ajax({
        url: '/my/category/list',
        success: function (res) {
            let str = ''
            res.data.forEach(item => {
                str += `
                <tr>
                <td>${item.name}</td>
                <td>${item.alias}</td>
                <td>
                    <button type="button" data-id=${item.id} data-name=${item.name} data-alias=${item.alias} class="layui-btn layui-btn-xs edit">编辑</button>
                    <button type="button" data-id=${item.id} class="layui-btn layui-btn-xs  layui-btn-danger del">删除</button>
                </td>
            </tr>
                `
            });
            $('tbody').html(str)

         }
    })
}
GetCategory()

//-------------------------添加类别---------------------------------
let addIndex 
$('#addCategory').on('click', function () {
    addIndex =  layer.open({
        type:1,
        title: '添加类别',
        content: $('#tip-add').html(),
        area:['500px','250px']
        });     
          
})
$('body').on('submit', '#add-category', function (e) {
    e.preventDefault()
    let data = $(this).serialize()
    console.log(data);
    $.ajax({
        type: 'POST',
        url: '/my/category/add',
        data: data,
        success: function (res) {
            layer.msg(res.message)
            if (res.status === 0) {
                GetCategory()
                layer.close(addIndex)
                
            }
        }
    })
}) 
//-------------------------修改类别---------------------------------
let editIndex
$('body').on('click', '.edit', function () {
   let  data = $(this).data()
    editIndex =  layer.open({
        type:1,
        title: '编辑类别',
        content: $('#tip-edit').html(),
        area: ['500px', '250px'],
        
        success: function (res) {
            //数据回填
            let form = layui.form
            form.val('form',data)
            /* $('.mingcheng').val(data1.name)
            $('.bieming').val(data1.alias) */
        }
        });
        
    })

 
 $('body').on('submit', '#edit-category', function (e) {
     e.preventDefault()
     let data = $(this).serialize()
    // let data = $(this).serialize()+`&id=${data1.id}`
    // console.log(data);
    $.ajax({
        type: 'POST',
        url: '/my/category/update',
        data: data,
        success: function (res) {
            layer.msg(res.message)
            if (res.status === 0) {
                GetCategory()
                layer.close(editIndex)
            }
        }
    })
})







//-------------------------删除类别---------------------------------
$('body').on('click', '.del', function () {
    let id = $(this).data('id')
    $.ajax({
        url: `/my/category/delete?id=${id}`,
        success: function (res) {
            layer.msg(res.message)
            if (res.status===0) {
                GetCategory()
            }
          }
    })
})